<template>
    <div>
      <div style="padding: 10px 0;">
      <el-input style="width: 200px;" placeholder="请输入姓名搜索" v-model="userName" suffix-icon="el-icon-search" >
      </el-input>
      <el-button type="primary" style="margin-left: 10px;" @click="load()"> 搜索</el-button>
      <el-button type="primary" style="margin-left: 10px;" @click="reset()"> 重置</el-button>
      </div>
      <div style="padding: 10px 0;">
      <el-button type="primary" style="margin-left: 10px;" @click="add()"> 新增</el-button>
      <el-button type="primary" style="margin-left: 10px;" @click="delAll()"> 批量删除</el-button>
      </div>

<el-dialog title="新增用户" :visible.sync="dialogFormVisible" width="30%">
    <el-form label-width="100px" :model="form" size="small">
         <el-form-item label="用户名称" >
            <el-input v-model="form.userName" autocomplete="off"></el-input>
         </el-form-item>

         <el-form-item label="用户密码" >
            <el-input v-model="form.password" autocomplete="off"></el-input>
         </el-form-item>
    </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="addUser()" >确 定</el-button>
  </div>
</el-dialog>

<el-dialog title="修改用户" :visible.sync="editFormVisible" width="30%">
    <el-form label-width="100px" :model="form" size="small">
         <el-form-item label="用户名称" >
            <el-input v-model="form.userName" autocomplete="off"></el-input>
         </el-form-item>

         <el-form-item label="用户密码" >
            <el-input v-model="form.password" autocomplete="off"></el-input>
         </el-form-item>
         <el-form-item label="角色" >
          <el-select clearable v-model="form.roleName" style="width: 100%;"  placeholder="请选择角色">
           <el-option
             v-for="item in options"
             :key="item.roleDesc"
             :label="item.roleDesc"
             :value="item.roleName">
           </el-option>
         </el-select>
         </el-form-item>

    </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="editFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="editUser()" >确 定</el-button>
  </div>
</el-dialog>

      <el-table :data="tableData" :border="true">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="userId" label="编号" v-if="false">
        </el-table-column>
        <el-table-column prop="userName" label="用户姓名">
        </el-table-column>
        <el-table-column prop="password" label="用户密码" >
        </el-table-column>
        <el-table-column prop="roleName" label="角色" >
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
                <el-button type="text" size="small" @click="edit(scope.row)">修改</el-button>
                <el-popconfirm
                  style="margin-left: 5px;"
                  confirm-button-text='好的'
                  cancel-button-text='不用了'
                  icon="el-icon-info"
                  icon-color="red"
                  @confirm="del(scope.row)"
                  title="您确定删除吗？">
                  <el-button type="text" slot="reference" size="small">删除</el-button>
                 </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>

  <div class="block" style="padding: 20px 0;">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[2, 5, 10, 15]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total=total>
    </el-pagination>
  </div>
   </div>
</template>
<script>
import { getUserListByPost, save, update, delUser } from '../api/User.js'
import { findAll } from '../api/Role.js'

export default {
  data () {
    return {
      tableData: [],
      multipleSelection: [],
      flag: false,
      asidWith: '',
      iconFold: 'el-icon-s-fold',
      total: 0,
      pageNum: 1,
      pageSize: 2,
      userName: '',
      dialogFormVisible: false,
      editFormVisible: false,
      options: [],
      form: {
        userId: '',
        userName: '',
        password: '',
        roleName: ''
      },
      upload: {
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/excel/import'
      }
    }
  },
  created () {
    this.load();
  },
  computed: {
  },
  methods: {
    load () {
      const params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        orderName: this.orderName
      }
      getUserListByPost(params).then(res => {
        this.tableData = res.data
        this.total = res.total
      }).catch(err => {
        console.log(err)
      })
    },

    reset () {
      this.orderName = '';
      this.load();
    },
    resetForm () {
      this.form = {
        orderName: '',
        orderMap: '',
        orderCount: ''
      }
    },
    handleSizeChange (pageSize) {
      console.log(pageSize);
      this.pageSize = pageSize
      this.load();
    },
    handleCurrentChange (pageNum) {
      console.log(pageNum);
      this.pageNum = pageNum
      this.load();
    },
    add () {
      this.form = {}
      this.dialogFormVisible = true
    },
    del (row) {
      delUser(row).then(res => {
        this.$message.success('删除成功')
        this.load()
      })
    },
    edit (row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.editFormVisible = true;
      findAll().then(res => {
        this.options = res
      })
    },
    editUser () {
      console.log('表单为', this.form)
      update(this.form).then(res => {
        this.$message.success('修改成功')
      }).catch(err => {
        console.log(err)
      });
      this.editFormVisible = false
      this.resetForm()
      this.load()
    },
    search () {
      console.log('搜索')
    },
    addUser () {
      save(this.form).then(res => {
        this.$message.success('添加成功')
      })
      this.dialogFormVisible = false
      this.resetForm()
      this.load()
    },
    delAll () {
      this.multipleSelection.forEach(item => {
        delUser(item).then(res => {
          this.$message.success('删除成功')
          this.load()
        })
      })
    }

  }
}
</script>
<style lang="scss" scoped>

</style>
